package com.sunlake.spring.main.model.auth.controller;

import com.sunlake.spring.main.common.response.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liuhanzhi49
 */
@RestController
@RequestMapping("/api/role-test")
public class RoleTestController {

    // 所有登录用户可访问
    @GetMapping("/user")
    public R userAccess() {
        return R.ok(200, "普通用户访问成功");
    }

    // 只有ADMIN角色可访问
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/admin")
    public R adminAccess() {
        return R.ok(200, "管理员访问成功");
    }

    // 只有COUNSELOR角色可访问
    @PreAuthorize("hasRole('COUNSELOR')")
    @GetMapping("/counselor")
    public R counselorAccess() {
        return R.ok(200, "顾问访问成功");
    }

    // 管理员或顾问可访问
    @PreAuthorize("hasAnyRole('ADMIN', 'COUNSELOR')")
    @GetMapping("/admin-or-counselor")
    public R adminOrCounselorAccess() {
        return R.ok(200, "管理员或顾问访问成功");
    }
}